package com.imooc.class09;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.Scope;

import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

public class MyScope implements Scope {
    private Map<String,Object> map1=new ConcurrentHashMap<>();
    private Map<String,Object> map2=new ConcurrentHashMap<>();

    @Override
    public Object get(String s, ObjectFactory<?> objectFactory) {
        if(map1.get(s)==null){
            map1.put(s,objectFactory.getObject());
            return map1.get(s);
        }
        if(map2.get(s)==null){
            map2.put(s,objectFactory.getObject());
            return map2.get(s);
        }

        int r=new Random().nextInt(2);
        if(r==0){
            return map1.get(s);
        }else{
            return map2.get(s);
        }

    }

    @Override
    public Object remove(String s) {
        if(map2.containsKey(s)){
            Object o=map2.get(s);
            map2.remove(s);
            return o;
        }
        if(map1.containsKey(s)){
            Object o=map1.get(s);
            map1.remove(s);
            return o;
        }
        return null;
    }

    @Override
    public void registerDestructionCallback(String s, Runnable runnable) {

    }

    @Override
    public Object resolveContextualObject(String s) {
        return null;
    }

    @Override
    public String getConversationId() {
        return null;
    }
}
